Rust Match Pattern Cheatsheet
·1 min
Remembering all the various patterns that can be used in a match expression can be quite tricky, so hereβs a free comprehensive list you can reference anytime π.
| Pattern | Example |
|---|---|
| Wildcard | _ |
| Literals | 300 βjohnβ |
| Range | 0 ..= 10 βaβ ..= βzβ |
| Variable | name mut name (Moves value into variable name) |
| ref Variable | ref name ref mut name |
| Reference | &value &(a, b) (Matches only reference values) |
| Tuple | (x, y, z) |
| Array | [r, g, b] |
| Slice | [a, b] [a, _, c] [a, .., z] |
| Enum | Some(value) None Day::Monday |
| Struct | Axis(x, y) Person {name, age} Profile {id, name, β¦} Shape {length: 10, active: true} |
| @ Binding | val @ 0 ..= 20 ref square @ Shape::Square {..} |
| Multiple patterns | βaβ | βbβ |
| Guard expression | x if x*x <= 10 |
Love to connect? Find me online on LinkedIn or Twitter. Bye!